package com.zw.pdm.module.designtask.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.designtask.DsnProStruct;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.designtask.mapper.ProStructMapper;
import com.zw.pdm.module.designtask.model.ProStructTastModel;
import com.zw.pdm.module.designtask.service.ProStructService;
import com.zw.pdm.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;

/**
 * @Description: 产品结构
 * 权限：
 * dsn:proCreate:view 查看
 * dsn:proCreate:edit 新增、编辑
 * dsn:proCreate:del 删除
 * dsn:proCreate:commit 提交
 *
 * dsn:proCheck:view 审核查看
 * dsn:proCheck:check 审核、驳回、撤销
 * @Author: China.wsg
 * @Date: 2020/8/14 14:47
 */
@Controller
@RequestMapping("/designTask/proStruct")
@SuppressWarnings("all")
public class ProStructController extends BaseController {

    @Autowired
    private ProStructService proStructService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private ProStructMapper proStructMapper;

    /**
     * 待创建产品结构页面
     *
     * @return
     */
    @GetMapping("/createProList")
    @RequiresPermissions({"dsn:proCreate:view"})
    public String proStructCreateList() {
        return "designtask/list/proStructCreate";
    }

    /**
     * 待创建产品结构列表（数据）
     */
    @ResponseBody
    @PostMapping("/createList")
    @RequiresPermissions({"dsn:proCreate:view"})
    public JsonResult taskList(ProStructTastModel proStructTastModel) {
        startPage();
        List<ProStructTastModel> list = proStructService.findProStructTastCreateByQuery(proStructTastModel);
        return jsonResult(list);
    }

    /**
     * 创建产品结构页面
     */
    @GetMapping("/createProStruct/{id}")
    @RequiresPermissions({"dsn:proCreate:edit"})
    public String info(@PathVariable("id") Integer proId, Model model) {
        ProStructTastModel proStructTastModel = new ProStructTastModel();
        proStructTastModel.setId(proId);
        model.addAttribute("proStructStatus", proStructService.findProStructStatusByQuery(proStructTastModel));
        model.addAttribute("proJect", proStructService.findProTastByQuery(proId).get(0));
        model.addAttribute("specTypes", proStructService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("specTypeId", proStructService.findOrgInfoByAccount());
        return "designtask/add/createProPage";
    }

    /**
     * 查看产品结构页面
     */
    @GetMapping("/viewProStruct")
    public String viewProPage(DsnProStruct dsnProStruct, Model model) {
        ProStructTastModel proStructTastModel = new ProStructTastModel();
        proStructTastModel.setId(dsnProStruct.getProId());
        model.addAttribute("proJect", proStructService.findProTastByQuery(dsnProStruct.getProId()).get(0));
        model.addAttribute("createCode", dsnProStruct.getCreateCode());
        model.addAttribute("specTypes", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "designtask/view/viewProPage";
    }

    /**
     * 保存产品结构
     *
     * @param checkPlan 保存信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"dsn:proCreate:edit"})
    public JsonResult proStructSave(DsnProStruct dsnProStruct) {
        Integer result = proStructService.proStructSave(dsnProStruct);
        if (result.compareTo(10) == 0) {
            return error("该部分已经存在，不允许重复添加");
        }
        return success();
    }

    /**
     * 修改产品结构
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"dsn:proCreate:edit"})
    public JsonResult proStructUpdate(DsnProStruct dsnProStruct) {
        Integer result = proStructService.proStructUpdate(dsnProStruct);
        if (result.compareTo(10) == 0) {
            return error("存在计划清单，不允许修改该部分");
        }
        if (result.compareTo(20) == 0) {
            return error("存在制作件清单，不允许修改该部分");
        }
        if (result.compareTo(30) == 0) {
            return error("该部分已经存在，不允许重复添加");
        }
        return success();
    }

    /**
     * 删除产品结构
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"dsn:proCreate:del"})
    public JsonResult proStructDel(Integer id) {
        Integer result = proStructService.proStructDel(id);
        if (result.compareTo(10) == 0) {
            return error("存在计划清单，不允许删除该部分");
        }
        if (result.compareTo(20) == 0) {
            return error("存在制作件清单，不允许删除该部分");
        }
        return success();
    }

    /**
     * 查看本人创建的产品结构
     */
    @ResponseBody
    @PostMapping("/proStructList")
    public JsonResult findProStructByQuery(DsnProStruct dsnProStruct) {
        startPage();
        List<DsnProStruct> list = proStructService.findProStructByQuery(dsnProStruct);
        return jsonResult(list);
    }

    /**
     * 提交产品结构待提交
     *
     * @param dsnProStruct
     * @return
     */
    @ResponseBody
    @PostMapping("/subProStruct")
    @RequiresPermissions({"dsn:proCreate:commit"})
    public JsonResult subProStruct(DsnProStruct dsnProStruct) {
        return jsonResult(proStructService.subProStructByProIdAndCreateCode(dsnProStruct));
    }

    /**
     * 待审核产品结构页面
     *
     * @return
     */
    @GetMapping("/checkProList")
    @RequiresPermissions({"dsn:proCheck:view"})
    public String checkProList(Model model) {
        model.addAttribute("specTypes", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("userName", ShiroUtils.getUser().getName());
        return "designtask/list/proStructCheck";
    }

    /**
     * 待审核产品结构列表（数据）
     */
    @ResponseBody
    @PostMapping("/checkList")
    @RequiresPermissions({"dsn:proCheck:view"})
    public JsonResult checkList(ProStructTastModel proStructTastModel) {
        startPage();
        List<ProStructTastModel> list = proStructService.findProStructTastCheckByQuery(proStructTastModel);
        return jsonResult(list);
    }

    /**
     * 审核产品结构页面
     */
    @GetMapping("/checkProStruct/{id}")
    @RequiresPermissions({"dsn:proCheck:view"})
    public String checkProPage(@PathVariable("id") Integer proId, Model model) {
        ProStructTastModel proStructTastModel = new ProStructTastModel();
        proStructTastModel.setId(proId);
        model.addAttribute("checkStatus", proStructMapper.findProStructCheckStatusByQuery(proId));
        model.addAttribute("proJect", proStructService.findProTastByQuery(proId).get(0));
        model.addAttribute("specTypes", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "designtask/check/checkProPage";
    }

    /**
     * 审核页面产品结构列表
     */
    @ResponseBody
    @PostMapping("/proStructCheckList")
    @RequiresPermissions({"dsn:proCheck:view"})
    public JsonResult findCheckProStructByQuery(Integer proId) {
        startPage();
        List<DsnProStruct> list = proStructService.findCheckProStructByQuery(proId);
        return jsonResult(list);
    }

    /**
     * 驳回产品结构
     *
     * @param dsnProStruct
     * @return
     */
    @ResponseBody
    @PostMapping("/reject")
    @RequiresPermissions({"dsn:proCheck:check"})
    public JsonResult proStructReject(DsnProStruct dsnProStruct) {
        return jsonResult(proStructService.rejectProStructByProIdAndCreateCode(dsnProStruct));
    }

    /**
     * 审核产品结构
     *
     * @param dsnProStruct
     * @return
     */
    @ResponseBody
    @PostMapping("/check")
    @RequiresPermissions({"dsn:proCheck:check"})
    public JsonResult proStructCheck(Integer proId) {
        boolean boolResult = proStructService.checkProStructByProId(proId);
        if (!boolResult) {
            return error("有产品结构尚未提交，操作失败！");
        } else {
            return success();
        }
    }

    /**
     * 撤销审核产品结构
     *
     * @param proId
     * @return
     */
    @ResponseBody
    @PostMapping("/cancel")
    @RequiresPermissions({"dsn:proCheck:check"})
    public JsonResult proStructCancel(Integer proId) {
        return jsonResult(proStructService.cancelProStructByProId(proId));
    }


}
